/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.domain;

import han.bifang.business.dao.CatalogBindingDao;
import han.bifang.business.dao.ServiceCatalogDao;
import han.bifang.business.dao.TreeDao;
import han.bifang.common.entity.CatalogBindingWrapper;
import han.bifang.common.entity.EntityConstants;
import han.bifang.common.entity.SoaCatalogDetail;
import han.bifang.common.entity.SoaServiceCatalog;
import han.bifang.common.entity.SoaTreeUnit;
import han.bifang.common.exception.BifangException;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServiceCatalogManager {
	private static Log logger = LogFactory.getLog(ServiceCatalogManager.class);

	public static ArrayList<String> createServiceCatalog(SoaTreeUnit t,
			SoaServiceCatalog b, ArrayList catBindings) throws BifangException {
		try {
			ArrayList<String> ret = new ArrayList<String>();
			;
			TreeDao.createTreeUnit(t);
			b.setUnitId(t.getId());
			ServiceCatalogDao.createServiceCatalog(b);
			t.setKey(b.getId());
			TreeDao.modifyTreeUnit(t);
			ret.add(t.getId());
			ret.add(b.getId());

			for (int i = 0, j = catBindings.size(); i < j; i++) {
				CatalogBindingWrapper cbw = (CatalogBindingWrapper) catBindings
						.get(i);
				cbw.getCatalogBinding().setCatalogId(b.getId());
				if (cbw.getCatalogBinding().getType().equals(
						EntityConstants.BINDING_HTTP)) {
					CatalogBindingDao.createBinding(cbw.getCatalogBinding(),
							cbw.getHttpBinding());
				} else if (cbw.getCatalogBinding().getType().equals(
						EntityConstants.BINDING_JMS)) {
					CatalogBindingDao.createBinding(cbw.getCatalogBinding(),
							cbw.getJmsBinding());
				} else if (cbw.getCatalogBinding().getType().equals(
						EntityConstants.BINDING_SOAP)) {
					CatalogBindingDao.createBinding(cbw.getCatalogBinding(),
							cbw.getSoapBinding());
				}

			}
			return ret;
		} catch (BifangException e) {
			throw e;

		}

	}

	public static void modifyServiceCatalog(SoaServiceCatalog b,
			ArrayList bindings) throws BifangException {
		try {
			ServiceCatalogDao.modifyServiceCatalog(b);
			SoaTreeUnit t = TreeDao.getTreeUnit(b.getUnitId());
			t.setName(b.getName());
			TreeDao.modifyTreeUnit(t);
			if (bindings.size() > 0) {
				CatalogBindingDao.removeBindingByCatalogId(b.getId());
				for (int i = 0, j = bindings.size(); i < j; i++) {
					CatalogBindingWrapper cbw = (CatalogBindingWrapper) bindings
							.get(i);
					cbw.getCatalogBinding().setCatalogId(b.getId());
					if (cbw.getCatalogBinding().getType().equals(
							EntityConstants.BINDING_HTTP)) {
						CatalogBindingDao.createBinding(
								cbw.getCatalogBinding(), cbw.getHttpBinding());
					} else if (cbw.getCatalogBinding().getType().equals(
							EntityConstants.BINDING_JMS)) {
						CatalogBindingDao.createBinding(
								cbw.getCatalogBinding(), cbw.getJmsBinding());
					} else if (cbw.getCatalogBinding().getType().equals(
							EntityConstants.BINDING_SOAP)) {
						CatalogBindingDao.createBinding(
								cbw.getCatalogBinding(), cbw.getSoapBinding());
					}

				}
			}
		} catch (BifangException e) {
			throw e;

		}

	}

	public static void removeServiceCatalog(String serviceCatalogId)
			throws BifangException {
		try {
			SoaServiceCatalog sc = ServiceCatalogDao
					.getServiceCatalog(serviceCatalogId);
			TreeDao.removeTreeUnit(sc.getUnitId());
			CatalogBindingDao.removeBindingByCatalogId(serviceCatalogId);
			ServiceCatalogDao.removeServiceCatalog(sc);
		} catch (BifangException e) {
			throw e;
		}

	}

	public static List getServiceCatalogByDomainId(String id)
			throws BifangException {
		try {

			return ServiceCatalogDao.getServiceCatalogByDomainId(id);

		} catch (BifangException e) {
			throw e;

		}

	}

	public static SoaCatalogDetail getServiceCatalogDetail(String id)
			throws BifangException {
		try {
			SoaCatalogDetail ret = new SoaCatalogDetail();

			ret.setServiceCatalog(ServiceCatalogDao.getServiceCatalog(id));
			ret.setBindings(CatalogBindingDao.getCatalogBindings(id));
			return ret;

		} catch (BifangException e) {
			throw e;

		}

	}

	public static SoaCatalogDetail getDefaultServiceCatalogDetail()
			throws BifangException {
		try {
			SoaCatalogDetail ret = new SoaCatalogDetail();

			ret.setServiceCatalog(ServiceCatalogDao.getDefaultServiceCatalog());
			ret.setBindings(CatalogBindingDao.getCatalogBindings(ret.getServiceCatalog().getId()));
			return ret;

		} catch (BifangException e) {
			throw e;

		}

	}

}
